﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Aliphant.ChatAI.Models;
using Aliphant.Mvvm.XEvent.UI;

namespace Aliphant.UI.AIChat.Models
{
    public class ChatAIConfig : BindableBase
    {
        public event Action? SaveEvent;

        public ChatAIConfig() { }

        public ChatAIConfig(ApiFactory factory)
        {
            this.Name = factory.name;
            this.API = factory.api;
            this.ApiKey = factory.key;
            this.ModelCollection = new ObservableCollection<string>(factory.models);
        }

        public void LoadConfig(ChatAISave config)
        {
            this.Model = config.model;
            this.Stream = config.stream;
        }

        public void ResetConfig()
        {
            this.Model = string.Empty;
            this.Stream = true;
        }

        /// <summary>
        /// 厂家
        /// </summary>
        private string _name = string.Empty;
        public string Name { get => _name; set => SetProperty(ref _name, value); }

        /// <summary>
        /// API
        /// </summary>
        private string _api = string.Empty;
        public string API { get => _api; set => SetProperty(ref _api, value); }

        /// <summary>
        /// 密钥
        /// </summary>
        private string _key = string.Empty;
        public string ApiKey { get => _key; set => SetProperty(ref _key, value); }


        #region 配置

        /// <summary>
        /// 模型
        /// </summary>
        private string _model = string.Empty;
        public string Model { get => _model; set => SetProperty(ref _model, value); }

        /// <summary>
        /// 流式传输
        /// </summary>
        private bool _stream = true;
        public bool Stream { get => _stream; set => SetProperty(ref _stream, value); }

        #endregion


        /// <summary>
        /// 模型列表
        /// </summary>
        public ObservableCollection<string> ModelCollection { get; private set; } = new ObservableCollection<string>();

        protected override bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string? propertyName = null)
        {
            var res = base.SetProperty(ref storage, value, propertyName);
            SaveEvent?.Invoke();
            return res;
        }

        public void Clone(ref GptConfig gpt)
        {
            gpt.API = this.API;
            gpt.Key = this.ApiKey;
            gpt.Model = this.Model;
            gpt.Stream = this.Stream;
        }
    }
}
